﻿using System;
using System.Text;

namespace Toolbox.util
{
    class HexUtils
    {

        public static byte[] hexStrToBytes(string hexStr)
        {
            // 16进制字符串转字节数组
            byte[] result = null;

            if (hexStr == null || hexStr.Length < 2)
            {
                return result;
            }

            //清除空格
            hexStr = hexStr.Replace(" ", "");

            int len = hexStr.Length;
            if ((len & 1) == 1)
            {
                // 是奇数,数据有问题
                return result;
            }

            int arrayLen = hexStr.Length / 2;
            result = new byte[arrayLen];
            for (int i=0; i<arrayLen; i++)
            {
                result[i] = Convert.ToByte(hexStr.Substring(i * 2, 2), 16);
            }

            return result;

        }

        public static string bytesToHexStr(byte[] bytes)
        {
            // 字节数组转16进制字符串
            string result = "";
            if (bytes == null || bytes.Length == 0)
            {
                return result;
            }

            StringBuilder sb = new StringBuilder();

            foreach (byte b in bytes)
            {
                sb.Append(b.ToString("X2"));
            }

            result = sb.ToString();
            return result;

        }

        public static float BytesToInt(byte[] bytes)
        {
            // 字节数组转int32
            if (bytes.Length < 4 || bytes.Length > 4)
            {
                //throw new NotEnoughDataInBufferException(data.length(), 8);
                return 0;
            }

            return BitConverter.ToInt32(bytes, 0);
        }

        public static string IntToHexStr(int data)
        {
            // int32转16进制字符串
            return Convert.ToString(data, 16);
            //return bytesToHexStr(BitConverter.GetBytes(data));
        }

        public static int HexStrToInt(string hexStr)
        {
            // 16进制字符串转int32
            return Convert.ToInt32(hexStr, 16);
            //return BitConverter.ToInt32(hexStrToBytes(hexStr), 0);
        }

        public static float BytesToFloat(byte[] bytes)
        {
            // 字节数组转浮点数
            if (bytes.Length < 4 || bytes.Length > 4)
            {
                //throw new NotEnoughDataInBufferException(data.length(), 8);
                return 0;
            }

            return BitConverter.ToSingle(bytes, 0);
        }

        public static string FloatToHexStr(float data)
        {
            // 浮点数转16进制字符串
            return bytesToHexStr(BitConverter.GetBytes(data));
        }

        public static float HexStrToFloat(string hexStr)
        {
            // 16进制字符串转浮点数
            return BitConverter.ToSingle(hexStrToBytes(hexStr), 0);
        }

        public static string spaceHexStr(string hexStr)
        {
            // 将16进制字符串加空格格式化，方便观察
            string result = "";

            if(hexStr == null || hexStr.Length == 0)
            {
                return result;
            }

            hexStr = hexStr.ToUpper();

            StringBuilder sb = new StringBuilder();
            for(int i=0; i<hexStr.Length; i += 2)
            {
                sb.Append(hexStr.Substring(i, 2)).Append(" ");
            }

            result = sb.ToString();
            return result;
        }
    }
}
